
package com.insigma.business.qrcode.soldier.service.impl;


import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.common.util.AdminAreaCodeUtils;
import com.insigma.business.qrcode.soldier.dao.CareSoldierVisitedRecordDao;
import com.insigma.business.qrcode.soldier.dto.CareSoldierVisitedRecordBSearcher;
import com.insigma.business.qrcode.soldier.dto.CareVisitUnifyRecordBO;
import com.insigma.business.qrcode.soldier.dto.SequenceNameConstant;
import com.insigma.business.qrcode.soldier.entity.CareSoldierVisitedRecordDO;
import com.insigma.business.qrcode.soldier.entity.DataStatsCDO;
import com.insigma.business.qrcode.soldier.service.CareSoldierVisitedRecordService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("careSoldierVisitedRecordService")
public class CareSoldierVisitedRecordServiceImpl implements CareSoldierVisitedRecordService {

    @Autowired(required = false)
    private CareSoldierVisitedRecordDao careSoldierVisitedRecordDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    
    @Override
    public List<CareSoldierVisitedRecordDO> search(final CareSoldierVisitedRecordBSearcher careSoldierVisitedRecordBSearcher) {
        if (Strings.isNotBlank(careSoldierVisitedRecordBSearcher.getAdminAreaCode())) {
            careSoldierVisitedRecordBSearcher.setAdminAreaCode(AdminAreaCodeUtils.getCutdAreaCode(careSoldierVisitedRecordBSearcher.getAdminAreaCode()));
        }
        return careSoldierVisitedRecordDao.search(careSoldierVisitedRecordBSearcher);
    }

    
    @Override
    public CareSoldierVisitedRecordDO get(final String id) {
        return careSoldierVisitedRecordDao.get(id);
    }

    
    @Override
    public Integer insert(final CareSoldierVisitedRecordDO careSoldierVisitedRecordDO) {
        careSoldierVisitedRecordDO.setId(sequenceGenerator.genKey(SequenceNameConstant.CARE_SOLDIER_VISITED_RECORD_ID));
        return careSoldierVisitedRecordDao.insert(careSoldierVisitedRecordDO);
    }

    
    @Override
    public Integer update(final CareSoldierVisitedRecordDO careSoldierVisitedRecordDO) {
        return careSoldierVisitedRecordDao.update(careSoldierVisitedRecordDO);
    }

    
    @Override
    public Integer delete(final String id, final String modifierType, final String modifierId) {
        return careSoldierVisitedRecordDao.delete(id, modifierType, modifierId);
    }

    @Override
    public CareSoldierVisitedRecordDO getByFormInstId(String formInstId) {
        return careSoldierVisitedRecordDao.getByFormInstId(formInstId);
    }

    @Override
    public Integer count(CareSoldierVisitedRecordBSearcher bSearcher) {
        Integer count = careSoldierVisitedRecordDao.count(bSearcher);
        if (count == null) {
            count = 0;
        }
        return count;
    }

    @Override
    public CareSoldierVisitedRecordDO getByBizId(String bizId) {
        return careSoldierVisitedRecordDao.getByBizId(bizId);
    }

    @Override
    public Integer countDifficulty(String adminAreaCode) {
        return careSoldierVisitedRecordDao.countDifficulty(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
    }

    @Override
    public List<DataStatsCDO> groupDifficultyVisitType(String adminAreaCode) {
        return careSoldierVisitedRecordDao.groupDifficultyVisitType(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
    }

    @Override
    public List<DataStatsCDO> groupDifficultyVisitAreaMap(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return careSoldierVisitedRecordDao.groupDifficultyVisitAreaMap(areaLike, nextLevel, levelLength);
    }

    @Override
    public List<DataStatsCDO> groupVisitAreaMap(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return careSoldierVisitedRecordDao.groupVisitAreaMap(areaLike, nextLevel, levelLength, null);
    }

    @Override
    public Integer isVisitedOfMonth(String idCardNo, String month) {
        return careSoldierVisitedRecordDao.countVisitedOfMonth(idCardNo, month);
    }

    @Override
    public CareSoldierVisitedRecordDO getNewestByIdCardNo(String idCardNo) {
        return careSoldierVisitedRecordDao.getNewestByIdCardNo(idCardNo);
    }

    @Override
    public List<CareVisitUnifyRecordBO> searchUnifyRecord(CareSoldierVisitedRecordBSearcher searcher) {
        if (Strings.isNotBlank(searcher.getAdminAreaCode())) {
            searcher.setAdminAreaCode(AdminAreaCodeUtils.getCutdAreaCode(searcher.getAdminAreaCode()));
        }
        return careSoldierVisitedRecordDao.searchUnifyRecord(searcher);
    }

    @Override
    public List<DataStatsCDO> groupEvaluationCountMap(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return careSoldierVisitedRecordDao.groupVisitAreaMap(areaLike, nextLevel, levelLength, true);
    }

    @Override
    public List<DataStatsCDO> groupEvaluationSumMap(String adminAreaCode) {
        int level = AdminAreaCodeUtils.getLevel(adminAreaCode);
        String areaLike = AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode) + "%";
        int nextLevel = level + 1;
        int levelLength = AdminAreaCodeUtils.getLevelLength(nextLevel);
        return careSoldierVisitedRecordDao.groupEvaluationSumMap(areaLike, nextLevel, levelLength);
    }

    @Override
    public Integer countByAreaAndTime(String adminAreaCode, String time) {
        return careSoldierVisitedRecordDao.countByAreaAndTime(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode), time);
    }

    @Override
    public List<DataStatsCDO> groupEvaluationScoreMap(String adminAreaCode) {
        return careSoldierVisitedRecordDao.groupEvaluationScoreMap(AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode));
    }

}
